<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dita PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<dita>
  <topic id="topic_gpaa">
    <title>gp_array_agg</title>
    <body>
      <p>The <codeph>gp_array_agg</codeph> module introduces a parallel
        <codeph>array_agg()</codeph> aggregate function that you can use in
        Greenplum Database.</p>
      <p>The <codeph>gp_array_agg</codeph> module is a Greenplum Database extension.</p>
    </body>
    <topic id="topic_reg">
      <title>Installing and Registering the Module</title>
      <body>
        <p>The <codeph>gp_array_agg</codeph> module is installed when you install
          Greenplum Database. Before you can use the aggregate function defined in the
          module, you must register the <codeph>gp_array_agg</codeph> extension in each
          database where you want to use the function:</p>
        <codeblock>CREATE EXTENSION gp_array_agg;</codeblock>
        <p>Refer to <xref href="../../install_guide/install_modules.html"
            format="html" scope="external">Installing Additional Supplied Modules</xref>
          for more information.</p>
      </body>
    </topic>
    <topic id="topic_use">
      <title>Using the Module</title>
      <body>
        <p>The <codeph>gp_array_agg()</codeph> function has the following
          signature:</p>
        <codeblock>gp_array_agg( anyelement )</codeblock>
        <p>You can use the function to create an array from input values, including nulls.
          For example:</p>
        <codeblock>SELECT gp_array_agg(a) FROM t1;
   gp_array_agg   
------------------
 {2,1,3,NULL,1,2}
(1 row)</codeblock>
        <p><codeph>gp_array_agg()</codeph> assigns each input value to an array element,
          and then returns the array. The function returns null rather than an empty array
          when there are no input rows.</p>
        <p><codeph>gp_array_agg()</codeph> produces results that depend on the ordering of
          the input rows. The ordering is unspecified by default; you can control the
          ordering by specifying an <codeph>ORDER BY</codeph> clause within the aggregate.
          For example:</p>
        <codeblock>CREATE TABLE table1(a int4, b int4);
INSERT INTO table1 VALUES (4,5), (2,1), (1,3), (3,null), (3,7);
SELECT gp_array_agg(a ORDER BY b NULLS FIRST) FROM table1;
  gp_array_agg  
--------------
 {3,2,1,4,7}
(1 row)</codeblock>
      </body>
    </topic>
    <topic id="topic_info">
      <title>Additional Module Documentation</title>
      <body>
        <p>Refer to <xref href="https://www.postgresql.org/docs/9.4/functions-aggregate.html" format="html"
          scope="external">Aggregate Functions</xref> in the PostgreSQL documentation for
          more information about aggregates.</p>
      </body>
    </topic>
  </topic>
</dita>
